package com.qingmeng;

import java.util.ArrayList;
import java.util.Scanner;

public class JavaMain {
    public static void main(String[] args) {
        ArrayList<Student> array = new ArrayList<>();

        while (true) {
            System.out.println("---欢迎使用学生管理系统---");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查看所有学生");
            System.out.println("5.教室座位检索");
            System.out.println("6.退出");
            System.out.println("请输入你的选择: ");

            Scanner sc = new Scanner(System.in);
            String line = sc.nextLine();
            switch (line) {
                case "1":
                    System.out.println("添加学生操作");
                    addStudent(array);
                    break;
                case "2":
                    System.out.println("删除学生操作");
                    deleteStudent(array);
                    break;
                case "3":
                    System.out.println("修改学生操作");
                    updateStudent(array);
                    break;
                case "4":
                    System.out.println("查看所有学生操作");
                    findAllStudent(array);
                    break;
                case "5":
                    System.out.println("教室座位检索");
                    searchSeat(array);
                    break;
                case "6":
                    System.out.println("感谢您的使用");
                    System.exit(0);
            }

        }
    }
    public static void addStudent(ArrayList<Student> array) {
        Scanner sc = new Scanner(System.in);
        String number;
        while (true) {
            System.out.println("请输入学号");
            number = sc.nextLine();
            boolean flag = isUsed(array, number);
            if (flag == true) {
                System.out.println("学号已被使用，请重新输入");
            } else {
                break;
            }
        }
        System.out.println("请输入姓名");
        String name = sc.nextLine();
        System.out.println("请输入年龄");
        String age = sc.nextLine();
        System.out.println("请输入居住地");
        String address = sc.nextLine();

        Student s = new Student();
        s.setNumber(number);
        s.setName(name);
        s.setAge(age);
        s.setAddress(address);

        array.add(s);

        System.out.println("添加成功");
    }


    public static boolean isUsed(ArrayList<Student> array, String number) {
        boolean flag = false;
        for (int i = 0; i < array.size(); i++) {
            Student s = array.get(i);
            if (s.getNumber().equals(number)) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    public static void deleteStudent(ArrayList<Student> array) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的学号");
        String number = sc.nextLine();

        int index = -1;
        for (int i = 0; i < array.size(); i++) {
            Student s = array.get(i);
            if (s.getNumber().equals(number)) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            System.out.println("该学生信息不存在");
        } else {
            array.remove(index);
            System.out.println("删除成功");
        }
    }

    public static void updateStudent(ArrayList<Student> array) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要修改的学号");
        String number = sc.nextLine();


        System.out.println("请输入姓名");
        String name = sc.nextLine();
        System.out.println("请输入年龄");
        String age = sc.nextLine();
        System.out.println("请输入居住地");
        String address = sc.nextLine();

        Student s = new Student();
        s.setNumber(number);
        s.setName(name);
        s.setAge(age);
        s.setAddress(address);

        for (int i = 0; i < array.size(); i++) {
            Student student = array.get(i);
            if (student.getNumber().equals(number)) {
                array.set(i, s);
                break;
            }
        }
        System.out.println("修改学生信息成功");
    }

    public static void findAllStudent(ArrayList<Student> array) {
        if (array.size() == 0) {
            System.out.println("请添加信息再查询");
            return;
        }

        System.out.println("学号\t姓名\t年龄\t居住地");

        for (int i = 0; i < array.size(); i++) {
            Student s = array.get(i);
            String number = s.getNumber();
            String name = s.getName();
            String age = s.getAge();
            String address = s.getAddress();
            System.out.println(number + "\t" + name + "\t" + age + "岁\t" + address);
        }
    }

    public static void searchSeat(ArrayList<Student> array) {

        if (array.size() == 0) {
            System.out.println("教室内没有学生，请添加后查询");
            return;
        } else {
            System.out.println("请输入被查询座位学生的姓名");
            Scanner sc = new Scanner(System.in);
            String stu = sc.nextLine();
            for (int i = 0; i < array.size(); i++) {//用索引查询学生的座位
                Student s = array.get(i);
                if (s.getName().equals(stu)) {
                    int index = array.indexOf(s);
                    System.out.println("该学生在座位 " + index + 1+"，该座位已被占用");
                }
                else if (!s.getName().equals(stu)) {
                    System.out.println("该学生座位信息不存在，是空座位");
                }
            }
        }
    }//分配学生座位还可以在Student类里面直接定义一个座位变量，用类似boolean isUsed的方法判断座位是否被占用
}


